var bbc = null;
$.ajax({
  type: "GET",
  url: "/admin/index/getMenuOther", //orderModifyStatus
  dataType: "json",
  async: false,
  success: function(data) {

    bbc = data;
    var start = new swiperScroll({
      data: data,
      current: data[0].id
    })
  }
});

function swiperScroll(config) {
  var data = config.data;
  var current = config.current;
  var lefHtml = ''
  for (var key in data) {

    if (!!data[key].children) {

      lefHtml +=
        `
          <li data-id="${data[key].id}">
               <img src="${data[key].icon}" alt="">
             ${data[key].title}

          </li>

        `
    } else {
      lefHtml +=
        `
          <li data-id="${data[key].id}">
              <a href="${data[key].name}">  ${data[key].title}</a>

          </li>

        `
    }
  }

  $('.left-scroll').html(`
<ul class="one-level">
${lefHtml}
    </ul>
      `)

  selectRight(data, current);
}

function selectRight(data, id,childID) {

  var html = ''
  for (var i = 0; i < data.length; i++) {
    if (data[i].id == id) {

      $('.one-level li:nth-child(' + (i + 1) + ')').append(`
                    <span class="layui-nav-more triangle"></span>
            `)
      $('.one-level li:nth-child(' + (i + 1) + ')').addClass('active')
      for (var key in data[i].children) {

        var htmlChild = '';
        if (!!data[i].children[key].children) {
          for (var keys in data[i].children[key].children) {
              var name=null;
            (data[i].children[key].children[keys].id==childID)&&(name="active");
            htmlChild += `
              <li class=${name}><a href="/${data[i].children[key].children[keys].name}">${data[i].children[key].children[keys].title}</a></li>
              `
          }
        }
        var h4 = !!data[i].children[key].children ? ` <h4>${data[i].children[key].title}</h4>` : `  <h4><a href="/${data[i].children[key].name}">${data[i].title}</a></h4>`
        html += `
            <li>
              ${h4}
              <ul>
                ${htmlChild}
              </ul>
            </li>
          `
      }
    } else {
      $('.one-level li:nth-child(' + (i + 1) + ') .triangle').remove();
      $('.one-level li:nth-child(' + (i + 1) + ')').removeClass('active');
    }
  }
  $('.right-scroll').html(`
      <ul class="subset">
        ${html}
        </ul>
    `)
}

$('.layui-side  ').on('click', '.one-level li', function() {
  selectRight(bbc, $(this).attr('data-id'))
})

var pid = location.href.substring(location.href.indexOf('?'), location.href.indexOf('&') == -1 ? location.href.length : location.href.indexOf('&'));
pid = pid.slice(pid.indexOf('=') + 1)
let url = window.location.href;
url = url.slice(url.indexOf(window.location.host) + window.location.host.length + 1);
if (url.indexOf('?') != -1) {
  url = url.slice(0, url.indexOf('?'));
}


$.ajax({
  type: "GET",
  url: "/admin/index/getUrlPid?url="+url, //orderModifyStatus
  dataType: "json",
  async: false,
  success: function(res) {

      selectRight(bbc,res.data.find_id,res.data.top_id);
  }
});
